#include "Computer.h"

Computer::Computer(string name) {
    this->name = name;
}

vector<bit> Computer::Send(string message) {
	/*
	 *  Send message.
	 */
    vector<bit> info = Util::Encode(message);
    string mode = "DOWN";

    cout << name + " is sending message: " + message << endl;

    cout << "    Passing application layer" << endl;
    // Application Layer
    applicationLayer.Recv(info);
    applicationLayer.Exec(mode);
    info = applicationLayer.Send();

    cout <<"    Passing transport layer" << endl;
    // Transport Layer
    transportLayer.Recv(info);
    transportLayer.Exec(mode);
    info = transportLayer.Send();

    cout << "    Passing network layer" << endl;
    // Network Layer
	
    networkLayer.Recv(info);
    networkLayer.Exec(mode);
    info = networkLayer.Send();
	
    cout << "    Passing link layer" << endl;
    // Link Layer
    linkLayer.Recv(info);
    linkLayer.Exec(mode);
    info = linkLayer.Send();

    cout << "    Passing physical layer" << endl;
    // Physical Layer
    physicalLayer.Recv(info);
    physicalLayer.Exec(mode);
    info = physicalLayer.Send();

    cout << "Finished" << endl;
    return info;
}

string Computer::Recv(vector<bit> info) {
    string mode = "UP";

    cout << name + " is receving message" <<endl;
    cout<<"    Passing physical layer"<<endl;
    // Physical Layer
    physicalLayer.Recv(info);
    physicalLayer.Exec(mode);
    info = physicalLayer.Send();

    cout<<"    Passing link layer"<<endl;
    // Link Layer
    linkLayer.Recv(info);
    linkLayer.Exec(mode);
    info = linkLayer.Send();

    cout<<"    Passing network layer"<<endl;
    // Network Layer
    networkLayer.Recv(info);
    networkLayer.Exec(mode);
    info = networkLayer.Send();

    cout<<"    Passing transport layer"<<endl;
    // Transport Layer
    transportLayer.Recv(info);
    transportLayer.Exec(mode);
    info = transportLayer.Send();

    cout<<"    Passing application layer"<<endl;
     // Application Layer
    applicationLayer.Recv(info);
    applicationLayer.Exec(mode);
    info = applicationLayer.Send();
    cout<<"Finished"<<endl;
    return Util::Decode(info);
}
